<?php
/**
 * This file is lil-wide controller file. 
 *
 * @copyright     Copyright 2008-2011, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
// init plugins and view
App::uses('LilPluginRegistry', 'Lil.Lib');
App::uses('LilAppView', 'Lil.View'); App::load('LilAppView');
/**
 * LilAppController class
 *
 * @uses          AppController
 * @package       lil
 * @subpackage    lil
 */
class LilAppController extends AppController {
/**
 * components property
 *
 * @var array
 */
	public $components = array('Session', 'Auth', 'Cookie');
/**
 * helpers property
 *
 * @var array
 */
	public $helpers = array('Session', 'Form', 'Html', 'Text', 'Time', 'Number', 'Cache', 'Lil.Lil', 'Lil.LilForm');
/**
 * viewClass property
 *
 * @var string
 */
	public $viewClass = 'LilApp';
/**
 * __construct method
 *
 * @return void
 */
	public function __construct($request = null, $response = null) {
		parent::__construct($request, $response);
		
		// set current area
		App::uses('LilCurrentArea', 'Lil.Lib');
		$this->currentArea = LilCurrentArea::getInstance();
		
		// set current user
		App::uses('LilCurrentUser', 'Lil.Lib');
		$this->currentUser = LilCurrentUser::getInstance();
		
		// create LilDate func
		App::uses('LilDateEngine', 'Lil.Lib');
		$this->LilDate = LilDateEngine::getInstance();
		
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		$registry->callPluginHandlers($this, 'after_construct_controller');
	}

/**
 * beforeFilter method
 *
 * @return void
 */
	public function beforeFilter() {
		// set language
		$lang = null;
		if (isset($this->request->query['lang'])) $lang = $this->request->query['lang'];
		elseif (isset($this->request->params['named']['lang'])) $lang = $this->request->params['named']['lang'];
		else $lang = $this->Cookie->read('Lil.lang');
		
		if (!empty($lang) && in_array($lang, Configure::read('Lil.languages'))) {
			Configure::write('Config.language', $lang);
			$this->Cookie->write('Lil.lang', $lang);
		}
		
		// init area :: read properties form request
		// note: request is not set in __construct so we must initialize it here
		$this->currentArea->initialize($this->request);
		
		// do init
		parent::beforeFilter();
		
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		$registry->callPluginHandlers($this, 'before_filter');
		$registry->callPluginHandlers($this, implode('_', array(
			'before',
			$this->request->params['controller'],
			$this->request->params['action']
		)));
	}
/**
 * afterFilter method
 *
 * @return void
 */
	public function afterFilter() {
		parent::afterFilter();
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		$registry->callPluginHandlers($this, 'after_filter');
	}
/**
 * callPluginHandlers method
 *
 * @return mixed
 */
	public function callPluginHandlers($handler, $data = null) {
		$registry = LilPluginRegistry::getInstance();
		return $registry->callPluginHandlers($this, $handler, $data);
	}
/**
 * beforeRender method
 *
 * @return void
 */
	public function beforeRender() {
		// set current area info
		$this->set('admin_area', $this->currentArea->get());
		
		// read default sidebar and allow plugins to modify it
		App::uses('LilSidebar', 'Lil.Lib');
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		
		$sidebar = new LilSidebar($this);
		$sidebar = $registry->callPluginHandlers($this, 'admin_sidebar', $sidebar->build());
		
		$this->set('admin_sidebar', $sidebar);
		
		// set admin layout
		if(
			(!empty($this->request->params['admin'])) && 
			($this->layout == 'default') &&
			($useLayout = Configure::read('Lil.useAdminLayout'))
		) {
			$this->layout = 'lil_admin';
			if (is_string($useLayout)) $this->layout = $useLayout;
		}
		
		parent::beforeRender();
		
		if (!empty($_POST['lil_submit'])) {
			$this->layout = 'popup_iframe';
			unset($_POST['lil_submit']);
		}
		
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		$registry->callPluginHandlers($this, 'before_render');
	}
/**
 * isAuthorized method
 *
 * @return void
 */
	public function isAuthorized() {
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		return $registry->callPluginHandlers($this, 'is_authorized', true);
	}
/**
 * redirect function
 *
 * @param string $url
 * @param integer $status
 * @param boolean $exit
 * @param boolean $redirectOnAjax
 * @return string
 */
	public function redirect($url, $status = null, $exit = true, $redirectOnAjax = false) {
		if ((!$this->request->is('ajax') && empty($_POST['lil_submit'])) || $redirectOnAjax) {
			parent::redirect($url, $status, $exit);
		} else {
			$this->autoRender = false;
			//$html = $this->render('popups' . DS . $this->request->params['action'], 'ajax');
			$html = $this->render('popups' . DS . $this->view, 'popup');
		}
	}
/**
 * doRedirect function
 *
 * @param string $fallback_url
 * @return void
 */
	public function doRedirect($fallback_url = array(), $options = array()) {
		$default_options = array('override' => false, 'modelClass' => $this->modelClass);
		
		// previous versions compatibility
		if (is_array($options)) {
			$options = array_merge($default_options, $options);
		} else {
			$default_options['override'] = (bool)$options;
			$options = $default_options;
		}
		
		$referer = '/';
		
		if (!empty($this->request->query['referer'])) {
			$referer = base64_decode($this->request->query['referer']);
		} else if (!empty($this->request->data[$options['modelClass']]['referer'])) {
			$referer = base64_decode($this->request->data[$options['modelClass']]['referer']);
		}
		
		if ($referer != '/' && !$options['override']) {
			$this->redirect($referer);
		} else if (!empty($fallback_url)) {
			$this->redirect($fallback_url);
		} else {
			$this->redirect(array('action' => 'index'));
		}
	}
/**
 * setupRedirect function
 *
 * @return void
 */
	public function setupRedirect($modelClass = null) {
		if (empty($modelClass)) $modelClass = $this->modelClass;
		if (empty($this->request->data[$modelClass]['referer'])) {
			$this->request->data[$modelClass]['referer'] = base64_encode($this->referer());
		}
	}
/**
 * error404 function
 *
 * @return void
 */
	public function error404($title = null) {
		throw new NotFoundException($title);
	}
/**
 * setFlash function
 *
 * @param string $message
 * @param string $element
 * @param boolean $flashOnAjax
 * @return void
 */
	public function setFlash($message, $element = 'default', $flashOnAjax = false) {
		if ((!$this->request->is('ajax') && empty($_POST['lil_submit'])) || $flashOnAjax) {
			$this->Session->setFlash($message, $element);
		}
	}
}